package com.google.fbyoutube.client.widget;

import java.util.HashMap;
import java.util.Map;

import com.google.fbyoutube.client.YouTube;
import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.TextBox;

public class WidSearchBar extends Composite {

	final private String SEARCH_VIDEOS = "Type some words to search";
	final TextBox searchInput;
	
	public WidSearchBar() {
		SimplePanel contentWrapper = new SimplePanel();
		contentWrapper.addStyleName("search-box");
		
		HorizontalPanel panel = new HorizontalPanel();
		searchInput = new TextBox();
		searchInput.setWidth("220px");
		searchInput.setHeight("23px");
		searchInput.setText(SEARCH_VIDEOS);
		searchInput.addStyleName("input-placeholder");
		searchInput.addStyleName("input-margin");
		
		searchInput.addFocusHandler(new FocusHandler() {
			
			@Override
			public void onFocus(FocusEvent event) {
				searchInput.setText("");
				searchInput.removeStyleName("input-placeholder");
			}
		});
		
		searchInput.addBlurHandler(new BlurHandler() {
			
			@Override
			public void onBlur(BlurEvent event) {
				if (searchInput.getText() == null || searchInput.getText().isEmpty()) {
					searchInput.setText(SEARCH_VIDEOS);
					searchInput.addStyleName("input-placeholder");
				}
			}
		});
		
		searchInput.addKeyUpHandler(new KeyUpHandler() {
			
			@Override
			public void onKeyUp(KeyUpEvent event) {
				switch (event.getNativeKeyCode() ) {
				case KeyCodes.KEY_ENTER:
					searchVideo();
					break;
				case KeyCodes.KEY_ESCAPE:
					searchInput.setText("");
					break;
					default:
				}
			}
		});
		
		panel.add(searchInput);
		panel.setCellVerticalAlignment(searchInput, HasVerticalAlignment.ALIGN_MIDDLE);
		
		Button searchButton = new Button("Search", new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				searchVideo();
			}

		});
		searchButton.setWidth("60px");
		searchButton.setHeight("35px");
		
		panel.add(searchButton);
		
		contentWrapper.add(panel);
		
		initWidget(contentWrapper);
	}
	
	private void searchVideo() {
		String searchInput = getInputText();
		Map<String, String> options = new HashMap<String, String>();
		options.put("q", searchInput);
		
		YouTube.getAppGUI().resetVideoList(null, null, null, options);
	}
	
	public String getInputText() {
		if (searchInput != null && !searchInput.getText().isEmpty() && !searchInput.getText().equals(SEARCH_VIDEOS)) {
			return escapeUrlString(searchInput.getText());
		} else {
			return null;
		}
	}
	
	private String escapeUrlString(String text) {
		String rc = text.replace(" ", "+");
		return rc;
	}
}
